using System;
using System.Reflection;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Microsoft.SharePoint;
using System.Globalization;
using System.Drawing.Design;
using System.Windows.Forms;
using Microsoft.SharePoint.Administration;

namespace SPVisualDev
{
    internal class PropSheetMainInfo : PropSheetBase
    {
        public PropSheetMainInfo(string DefaultResourceFile, EnvDTE.ProjectItem ParentFeatureFolder, XmlElement ElementXml)
        {
            this.DefaultResourceFile = DefaultResourceFile;
            this.ParentFeatureFolder = ParentFeatureFolder;

            this.Title = new ResourceStringContainer(string.Empty, DefaultResourceFile, ParentFeatureFolder);
            this.Description = new ResourceStringContainer(string.Empty, DefaultResourceFile, ParentFeatureFolder);

            if (ElementXml != null)
                this.PropXmlNode = ElementXml;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Title"), RefreshProperties(RefreshProperties.None), TypeConverter(typeof(ResourceExpandableObjectConverter))]
        public ResourceStringContainer Title
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Description"), RefreshProperties(RefreshProperties.None), TypeConverter(typeof(ResourceExpandableObjectConverter))]
        public ResourceStringContainer Description
        {
            get;
            set;
        }

        [CategoryAttribute("Main info"), DescriptionAttribute("Version"), RefreshProperties(RefreshProperties.None)]
        public string Version
        {
            get;
            set;
        }

        [CategoryAttribute("Main info"), DescriptionAttribute("Scope"), RefreshProperties(RefreshProperties.None)]
        public SPFeatureScope Scope
        {
            get;
            set;
        }

        [CategoryAttribute("Main info"), DescriptionAttribute("Hidden feature"), RefreshProperties(RefreshProperties.None)]
        public bool? Hidden
        {
            get;
            set;
        }

        [CategoryAttribute("Main info"), DisplayName("Event receiver"), DescriptionAttribute("Creates event receiver"), RefreshProperties(RefreshProperties.None)]
        public bool EventReceiver
        {
            get;
            set;
        }

        [Browsable(false)]
        public override string ElementFileName
        {
            get
            {
                return base.ElementFileName;
            }
            set
            {
                base.ElementFileName = value;
            }
        }
    }

    internal class PropSheetListInstance : PropSheetBase
    {
        public PropSheetListInstance(string DefaultResourceFile, EnvDTE.ProjectItem ParentFeatureFolder, XmlElement ElementXml)
        {
            this.DefaultResourceFile = DefaultResourceFile;
            this.ParentFeatureFolder = ParentFeatureFolder;

            this.Title = new ResourceStringContainer(string.Empty, DefaultResourceFile, ParentFeatureFolder);
            this.Url = new ResourceStringContainer(string.Empty, DefaultResourceFile, ParentFeatureFolder);

            if (ElementXml != null)
                this.PropXmlNode = ElementXml;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Instance ID"), RefreshProperties(RefreshProperties.None)]
        public int Id
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Title"), RefreshProperties(RefreshProperties.None), TypeConverter(typeof(ResourceExpandableObjectConverter))]
        public ResourceStringContainer Title
        {
            get;
            set;
        }


        [CategoryAttribute("Properties"), DescriptionAttribute("Url"), RefreshProperties(RefreshProperties.None), TypeConverter(typeof(ResourceExpandableObjectConverter))]
        public ResourceStringContainer Url
        {
            get;
            set;
        }

        private string m_TemplateType = "0";
        [CategoryAttribute("Properties"), DescriptionAttribute("Custom list template type"), RefreshProperties(RefreshProperties.None), TypeConverter(typeof(TemplateTypeExpandableConverter))]
        public string TemplateType
        {
            get
            {
                return m_TemplateType;
            }
            set
            {
                try
                {
                    //Test if it is a valid integer.
                    int i = int.Parse(value);

                    string s = Enum.GetName(typeof(SPListTemplateType), i);
                    if (s != null)
                    {
                        m_TemplateType = s;
                    }
                    else
                    {
                        m_TemplateType = value;
                    }

                }
                catch
                {
                    try
                    {
                        SPListTemplateType type = (SPListTemplateType)Enum.Parse(typeof(SPListTemplateType), value);
                        this.FeatureId = Common.GetFeatureIdForBuiltInListType((int)type).ToString().ToUpper();
                    }
                    catch { }

                    m_TemplateType = value;
                }

            }
        }


        [CategoryAttribute("Properties"), DescriptionAttribute("Feature ID"), RefreshProperties(RefreshProperties.None)]
        public string FeatureId
        {
            get;
            set;
        }

        public override string ToString()
        {
            string s = this.Url.GetValue(null);
            if (string.IsNullOrEmpty(s))
                s = this.ElementInfo.ElementName + this.ElementInfo.ID.ToString();

            return "<ListInstance> " + s;
        }

        [Browsable(false)]
        public override XmlElement PropXmlNode
        {
            get
            {
                XmlElement xel = base.PropXmlNode;

                try
                {
                    int.Parse(xel.GetAttribute("TemplateType"));
                }
                catch(FormatException)
                {
                    try
                    {
                        SPListTemplateType type = (SPListTemplateType)Enum.Parse(typeof(SPListTemplateType), xel.GetAttribute("TemplateType"), false);

                        xel.SetAttribute("TemplateType", ((int)type).ToString());
                    }
                    catch { }
                }


                return xel;
            }
            set
            {
                base.PropXmlNode = value;

                XmlElement xel = value;

                string s = xel.GetAttribute("Id");
                if (string.IsNullOrEmpty(s))
                {
                    this.Id = new Random().Next(65535);
                }
                else
                {
                    try
                    {
                        this.Id = int.Parse(s);
                    }
                    catch { }
                }

                /*
                SPListTemplateType type = SPListTemplateType.InvalidType;
                s = xel.GetAttribute("TemplateType");
                if (!string.IsNullOrEmpty(s))
                {
                    if (Enum.IsDefined(typeof(Microsoft.SharePoint.SPListTemplateType), int.Parse(s)))
                    {
                        type = (SPListTemplateType)Enum.Parse(typeof(SPListTemplateType), s);
                    }
                }

                this.TemplateBuiltInType = type;


                if (type != SPListTemplateType.InvalidType)
                {
                    this.TemplateType = ((int)type).ToString();
                }
                else
                {
                    this.TemplateType = s;
                }
                */
            }
        }

        public class TemplateTypeExpandableConverter : StringConverter
        {
            public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
            {
                return false;
            }

            public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
            {
                return true;
            }

            public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
            {
                StandardValuesCollection coll = new StandardValuesCollection(Enum.GetNames(typeof(SPListTemplateType)));
                return coll;
            }
        }
    }

	internal class PropSheetListTemplate : PropSheetBase
	{
        public PropSheetListTemplate(string DefaultResourceFile, EnvDTE.ProjectItem ParentFeatureFolder, XmlElement ElementXml)
        {
            this.DefaultResourceFile = DefaultResourceFile;
            this.ParentFeatureFolder = ParentFeatureFolder;

            this.DisplayName = new ResourceStringContainer(string.Empty, DefaultResourceFile, ParentFeatureFolder);
            this.Description = new ResourceStringContainer(string.Empty, DefaultResourceFile, ParentFeatureFolder);

            if (ElementXml != null)
                this.PropXmlNode = ElementXml;
        }

        private BaseTypeInfoStruct m_original_type;
        [CategoryAttribute("NewTemplate"), Browsable(true), DisplayName("Built-in template to start from"), DescriptionAttribute("Original type"), ReadOnly(false), TypeConverter(typeof(OriginalTypeExpandableConverter))]
        public BaseTypeInfoStruct OriginalType
        {
            get
            {
                return m_original_type;
            }
            set
            {
                m_original_type = value;

                this.BaseType = m_original_type.BaseType;
            }
        }

        [CategoryAttribute("NewTemplate"), Browsable(true), DisplayName("Create event receiver"), DescriptionAttribute("Create event receiver"), ReadOnly(false)]
        public bool EventReceivers
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Internal name"), RefreshProperties(RefreshProperties.None)]
        public string Name
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Display name"), RefreshProperties(RefreshProperties.None), TypeConverter(typeof(ResourceExpandableObjectConverter))]
        public ResourceStringContainer DisplayName
		{
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Description"), RefreshProperties(RefreshProperties.None), TypeConverter(typeof(ResourceExpandableObjectConverter))]
        public ResourceStringContainer Description
		{
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Base type"), RefreshProperties(RefreshProperties.None)]
        public int BaseType
		{
			get;
			set;
		}

        [CategoryAttribute("Properties"), DescriptionAttribute("List type"), RefreshProperties(RefreshProperties.None)]
        public int Type
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Security bits"), RefreshProperties(RefreshProperties.None)]
        public int SecurityBits
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("On quick launch"), RefreshProperties(RefreshProperties.None)]
        public bool? OnQuickLaunch
        {
            get;
            set;
        }

        /*
        [CategoryAttribute("Properties"), DescriptionAttribute("Sequence"), RefreshProperties(RefreshProperties.None)]
        public string Sequence
        {
            get;
            set;
        }
        */
        
        public void SetNewSheetFlag(bool IsNewSheet)
        {
            PropertyDescriptor descriptor = TypeDescriptor.GetProperties(this.GetType())["OriginalType"];
            BrowsableAttribute attrib = (BrowsableAttribute)descriptor.Attributes[typeof(BrowsableAttribute)];
            FieldInfo isBrowsable = attrib.GetType().GetField("browsable", BindingFlags.NonPublic | BindingFlags.Instance);
            isBrowsable.SetValue(attrib, IsNewSheet);

            descriptor = TypeDescriptor.GetProperties(this.GetType())["EventReceivers"];
            attrib = (BrowsableAttribute)descriptor.Attributes[typeof(BrowsableAttribute)];
            isBrowsable = attrib.GetType().GetField("browsable", BindingFlags.NonPublic | BindingFlags.Instance);
            isBrowsable.SetValue(attrib, IsNewSheet);

        }

        private XmlDocument _ImportedXml;
        [Browsable(false)]
        public XmlDocument ImportedXml
        {
            get { return _ImportedXml; }
            set { _ImportedXml = value; }
        }

		public override string ToString()
		{
            string s = this.Name;
            if (string.IsNullOrEmpty(s))
                s = this.ElementInfo.ElementName + this.ElementInfo.ID.ToString();

			return "<ListTemplate> " + s;
		}

		[Browsable(false)]
		public override XmlElement PropXmlNode
		{
			get
			{
                XmlElement xel = base.PropXmlNode;

                xel.SetAttribute("SecurityBits", (this.SecurityBits == 0) ? "11" : this.SecurityBits.ToString());
                xel.SetAttribute("OnQuickLaunch", this.OnQuickLaunch.ToString().ToUpper());

                /*
                if (!string.IsNullOrEmpty(this.Sequence))
                    xel.SetAttribute("Sequence", this.Sequence);
                */

				return xel;
			}
			set
			{
                XmlElement xel = value;

                /*
                if (!string.IsNullOrEmpty(this.Sequence))
                    this.Sequence = xel.GetAttribute("Sequence");
                */

                base.PropXmlNode = xel;
			}
		}

        public class OriginalTypeExpandableConverter : ExpandableObjectConverter
        {
            private List<BaseTypeInfoStruct> m_base_type_list = new List<BaseTypeInfoStruct>();

            public OriginalTypeExpandableConverter()
            {
                string sp_install_path = Common.GetSharePointInstallPath() + @"\TEMPLATE\FEATURES";

                m_base_type_list.Add(new BaseTypeInfoStruct("GenericList", 0, 100, "Generic list", sp_install_path + @"\CustomList\CustList"));
                m_base_type_list.Add(new BaseTypeInfoStruct("DocumentLibrary", 1, 101, "Document library", sp_install_path + @"\DocumentLibrary\DocLib"));
                m_base_type_list.Add(new BaseTypeInfoStruct("Survey", 4, 102, "Survey", sp_install_path + @"\SurveysList\Survey"));
                m_base_type_list.Add(new BaseTypeInfoStruct("Links", 0, 103, "Links", sp_install_path + @"\LinksList\Links"));
                m_base_type_list.Add(new BaseTypeInfoStruct("Announcements", 0, 104, "Announcements", sp_install_path + @"\AnnouncementsList\Announce"));
                m_base_type_list.Add(new BaseTypeInfoStruct("Contacts", 0, 105, "Contacts", sp_install_path + @"\ContactsList\Contacts" ));
                m_base_type_list.Add(new BaseTypeInfoStruct("Events", 0, 106, "Events", sp_install_path + @"\EventsList\Events"));
                m_base_type_list.Add(new BaseTypeInfoStruct("Tasks", 0, 107, "Tasks", sp_install_path + @"\TasksList\Tasks"));
                m_base_type_list.Add(new BaseTypeInfoStruct("DiscussionBoard", 0, 108, "Discussion board", sp_install_path + @"\DiscussionsList\Discuss" ));
                m_base_type_list.Add(new BaseTypeInfoStruct("PictureLibrary", 1, 109, "Picture library", sp_install_path + @"\PictureLibrary\PicLib"));
                m_base_type_list.Add(new BaseTypeInfoStruct("WebPageLibrary", 1, 119, "Web page library", sp_install_path + @"\WebPageLibrary\WebPageLib"));
                m_base_type_list.Add(new BaseTypeInfoStruct("CustomGrid", 0, 120, "Custom grid", sp_install_path + @"\GridList\GridList"));
                m_base_type_list.Add(new BaseTypeInfoStruct("GanttTasks", 0, 150, "Gantt tasks", sp_install_path + @"\GanttTasksList\Gantt"));
                m_base_type_list.Add(new BaseTypeInfoStruct("IssueTracking", 5, 1100, "Issue tracking", sp_install_path + @"\IssuesList\Issue"));
                m_base_type_list.Add(new BaseTypeInfoStruct("XMLForm", 1, 115, "XML Form library", sp_install_path + @"\XmlFormLibrary\XmlForm"));

            }

            public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
            {
                return true;
            }

            public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
            {
                return true;
            }

            public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
            {
                //StandardValuesCollection coll = new StandardValuesCollection(Enum.GetNames(typeof(PredefinedListTypeEnum)));
                return new StandardValuesCollection(m_base_type_list);
            }

            public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
            {
                return true;
            }

            public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
            {
                return true;
            }
            public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
            {
                if (value is string)
                {
                    BaseTypeInfoStruct info = m_base_type_list.Find(delegate(BaseTypeInfoStruct b) { return b.TemplateName == (value as string); });
                    return info;
                }
                else
                {
                    return base.ConvertFrom(context, culture, value);
                }
            }

            public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
            {
                if (value is BaseTypeInfoStruct && destinationType == typeof(string))
                {
                    if (context != null)
                        context.GetType().GetProperty("Expanded").SetValue(context, true, null);

                    BaseTypeInfoStruct info = (BaseTypeInfoStruct)value;
                    if (info.TemplateName == null)
                        return string.Empty;
                    else
                        return info.TemplateName;
                }
                else
                {
                    return base.ConvertTo(context, culture, value, destinationType);
                }
            }

            public override bool GetPropertiesSupported(ITypeDescriptorContext context)
            {
                return false;
            }
        }

	}

	internal class PropSheetContentType : PropSheetBase
	{
        public PropSheetContentType(string DefaultResourceFile, EnvDTE.ProjectItem ParentFeatureFolder, XmlElement ElementXml)
        {
            this.DefaultResourceFile = DefaultResourceFile;
            this.ParentFeatureFolder = ParentFeatureFolder;

            this.Name = new ResourceStringContainer(string.Empty, DefaultResourceFile, ParentFeatureFolder);
            this.Group = new ResourceStringContainer(string.Empty, DefaultResourceFile, ParentFeatureFolder);
            this.Description = new ResourceStringContainer(string.Empty, DefaultResourceFile, ParentFeatureFolder);

            if (ElementXml != null)
                this.PropXmlNode = ElementXml;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("ID"), RefreshProperties(RefreshProperties.None), Editor(typeof(IDSelector), typeof(UITypeEditor))]
        public string ID
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Content type name"), RefreshProperties(RefreshProperties.None), TypeConverter(typeof(ResourceExpandableObjectConverter))]
        public ResourceStringContainer Name
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Group name"), RefreshProperties(RefreshProperties.None), TypeConverter(typeof(ResourceExpandableObjectConverter))]
        public ResourceStringContainer Group
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Description"), RefreshProperties(RefreshProperties.None), TypeConverter(typeof(ResourceExpandableObjectConverter))]
        public ResourceStringContainer Description
        {
            get;
            set;
        }

        [CategoryAttribute("NewTemplate"), Browsable(true), DisplayName("Create event receiver"), DescriptionAttribute("Create event receiver"), ReadOnly(false)]
        public bool EventReceivers
        {
            get;
            set;
        }

		public override string ToString()
		{
            string s = this.Name.GetValue(null);
            if (string.IsNullOrEmpty(s))
            {
                s = this.ID;
                if (string.IsNullOrEmpty(s))
                    s = this.ElementInfo.ElementName + this.ElementInfo.ID.ToString();
            }

			return "<ContentType> " + s;
		}

        public void SetNewSheetFlag(bool IsNewSheet)
        {
            PropertyDescriptor descriptor = TypeDescriptor.GetProperties(this.GetType())["EventReceivers"];
            BrowsableAttribute attrib = (BrowsableAttribute)descriptor.Attributes[typeof(BrowsableAttribute)];
            FieldInfo isBrowsable = attrib.GetType().GetField("browsable", BindingFlags.NonPublic | BindingFlags.Instance);
            isBrowsable.SetValue(attrib, IsNewSheet);
        }

        public class IDSelector : UITypeEditor
        {
            public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
            {
                return UITypeEditorEditStyle.Modal;
            }
            public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
            {
                string ret = value as string;

                /*
                if (Common.CheckIf32bitOn64())
                {
                    MessageBox.Show("This feature is not supported on 64 bit SharePoint installation.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return ret;
                }
                */

                if (!Common.CheckIf32bitOn64())
                    if (!Common.CheckIfUserHasSuffAdminPrivOnFarm(true))
                        return ret;


                using (DialogNewContentTypeDef form = new DialogNewContentTypeDef())
                {
                    DialogResult res = form.ShowDialog();
                    if (res != DialogResult.OK)
                        return ret;


                    if (form.rdoInheritFromBuiltin.Checked)
                    {
                        using (DialogNewContentTypeFromBuiltIn form_builtin = new DialogNewContentTypeFromBuiltIn())
                        {
                            form_builtin.cboContentTypes.Items.Clear();
                            form_builtin.cboContentTypes.Sorted = true;
                            form_builtin.cboContentTypes.DisplayMember = "Key";
                            form_builtin.cboContentTypes.ValueMember = "Value";

                            if (Common.CheckIf32bitOn64())
                            {
                                string output;
                                int err = StsAdmCommands.ExecuteStsadmCommand("getallbuiltincontenttypes", "", out output);
                                if (err == 0)
                                {
                                    XmlDocument xmldoc = new XmlDocument();
                                    xmldoc.LoadXml(output);

                                    foreach (XmlNode node in xmldoc.SelectSingleNode("/XmlRoot/ContentTypes").ChildNodes)
                                    {
                                        XmlElement el = node as XmlElement;
                                        if (el == null)
                                            continue;

                                        KeyValuePair<string, string> kvp = new KeyValuePair<string, string>(el.GetAttribute("Name"), el.GetAttribute("Id"));
                                        form_builtin.cboContentTypes.Items.Add(kvp);
                                    }
                                }
                                else
                                {
                                    throw new Exception("Error occured: ");
                                }
                            }
                            else
                            {
                                SPFeatureDefinition def = SPFarm.Local.FeatureDefinitions["ctypes"];
                                foreach (SPElementDefinition element in def.GetElementDefinitions(System.Globalization.CultureInfo.CurrentCulture))
                                {
                                    if (element.ElementType == "ContentType")
                                    {
                                        KeyValuePair<string, string> kvp = new KeyValuePair<string, string>(element.XmlDefinition.Attributes["Name"].Value, element.XmlDefinition.Attributes["ID"].Value);
                                        form_builtin.cboContentTypes.Items.Add(kvp);
                                    }
                                }
                            }

                            form_builtin.cboContentTypes.SelectedIndex = 0;

                            res = form_builtin.ShowDialog();

                            if (res == DialogResult.OK && form_builtin.cboContentTypes.SelectedIndex != -1)
                            {
                                KeyValuePair<string, string> sel_kvp = (KeyValuePair<string, string>)form_builtin.cboContentTypes.SelectedItem;

                                if (form_builtin.chkOnlyAsParent.Checked)
                                    ret = sel_kvp.Value;
                                else
                                    ret = sel_kvp.Value + "00" + Guid.NewGuid().ToString("N").ToUpper();
                            }
                        }
                    }

                    else if (form.rdoInheritFromExistent.Checked)
                    {
                        using (DialogSharePointExplorer explorer = new DialogSharePointExplorer())
                        {
                            explorer.UseWOW64BitCompatibleMode = Common.CheckIf32bitOn64();
                            explorer.ShowContentTypes = true;
                            explorer.ShowContentTypeGroups = true;
                            explorer.ShowLists = false;
                            explorer.Message = "Pick a content type to inherit from.";

                            explorer.ucspExplorer1.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
                            explorer.ucspExplorer1.Margin = new Padding(0, 0, 0, 15);

                            CheckBox chkOnlyAsParent = new CheckBox();
                            chkOnlyAsParent.AutoSize = true;
                            chkOnlyAsParent.Location = new System.Drawing.Point(230, explorer.Height - 64);
                            chkOnlyAsParent.Name = "chkOnlyAsParent";
                            chkOnlyAsParent.Size = new System.Drawing.Size(108, 17);
                            chkOnlyAsParent.Margin = new Padding(0, 5, 0, 5);
                            chkOnlyAsParent.Text = "Only as parent ID";
                            chkOnlyAsParent.UseVisualStyleBackColor = true;
                            chkOnlyAsParent.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

                            explorer.Controls.Add(chkOnlyAsParent);

                            explorer.MinimumSize = new System.Drawing.Size(explorer.Width, explorer.Height);
                            DialogResult result = explorer.ShowDialog();
                            if (result != DialogResult.OK)
                                return ret;

                            string ct_id = explorer.SelectedContentTypeId;
                            if (ct_id != null)
                            {
                                if (!chkOnlyAsParent.Checked)
                                    ret = ct_id + "00" + Guid.NewGuid().ToString("N").ToUpper();
                                else
                                    ret = ct_id + "00";
                            }
                        }
                    }
                    else if (form.rdoAppendGUID.Checked)
                    {
                        if (string.IsNullOrEmpty(ret))
                            ret = "0x01";

                        ret += "00" + Guid.NewGuid().ToString("N").ToUpper();
                    }

                }

                return ret;
            }
        }
	}
    
	internal class PropSheetModule : PropSheetBase
	{
        public PropSheetModule(string DefaultResourceFile, EnvDTE.ProjectItem ParentFeatureFolder, XmlElement ElementXml)
        {
            this.DefaultResourceFile = DefaultResourceFile;
            this.ParentFeatureFolder = ParentFeatureFolder;

            //this.Name = new ResourceStringContainer(string.Empty, DefaultResourceFile, ParentFeatureFolder);
            //this.Path = new ResourceStringContainer(string.Empty, DefaultResourceFile, ParentFeatureFolder);
            this.Url = new ResourceStringContainer(string.Empty, DefaultResourceFile, ParentFeatureFolder);

            if (ElementXml != null)
                this.PropXmlNode = ElementXml;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Module name"), RefreshProperties(RefreshProperties.None)]
        public string Name
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Path"), RefreshProperties(RefreshProperties.None)]
        public string Path
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Url"), RefreshProperties(RefreshProperties.None), TypeConverter(typeof(ResourceExpandableObjectConverter))]
        public ResourceStringContainer Url
        {
            get;
            set;
        }

        private string _SubType;
        [Browsable(false)]
        public string SubType
        {
            get { return _SubType; }
            set { _SubType = value; }
        }

		public override string ToString()
		{
            string s = string.Empty;
            if (string.IsNullOrEmpty(this.Name))
            {
                if (!string.IsNullOrEmpty(this.Path))
                    s = this.Path;
            }
            else
            {
                s = this.Name;
            }

			return "<Module> " + s;
		}
	}

    internal class PropSheetCustomAction : PropSheetBase
    {
        public PropSheetCustomAction(string DefaultResourceFile, EnvDTE.ProjectItem ParentFeatureFolder, XmlElement ElementXml)
        {
            this.DefaultResourceFile = DefaultResourceFile;
            this.ParentFeatureFolder = ParentFeatureFolder;

            this.Title = new ResourceStringContainer(string.Empty, DefaultResourceFile, ParentFeatureFolder);
            this.Sequence = new ResourceStringContainer(string.Empty, DefaultResourceFile, ParentFeatureFolder);
            this.Description = new ResourceStringContainer(string.Empty, DefaultResourceFile, ParentFeatureFolder);

            if (ElementXml != null)
                this.PropXmlNode = ElementXml;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Title"), RefreshProperties(RefreshProperties.None), TypeConverter(typeof(ResourceExpandableObjectConverter))]
        public ResourceStringContainer Title
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("ID"), RefreshProperties(RefreshProperties.None)]
        public string Id
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Sequence"), RefreshProperties(RefreshProperties.None), TypeConverter(typeof(ResourceExpandableObjectConverter))]
        public ResourceStringContainer Sequence
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Location"), RefreshProperties(RefreshProperties.None)]
        public string Location
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Description"), RefreshProperties(RefreshProperties.None), TypeConverter(typeof(ResourceExpandableObjectConverter))]
        public ResourceStringContainer Description
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Group ID"), RefreshProperties(RefreshProperties.None)]
        public string GroupId
        {
            get;
            set;
        }

        public override string ToString()
        {
            string s = this.Title.GetValue(null);
            if (string.IsNullOrEmpty(s))
                s = this.ElementInfo.ElementName + this.ElementInfo.ID.ToString();

            return "<CustomAction> " + s;
        }

    }

    internal class PropSheetField : PropSheetBase
    {
        public PropSheetField(string DefaultResourceFile, EnvDTE.ProjectItem ParentFeatureFolder, XmlElement ElementXml)
        {
            this.DefaultResourceFile = DefaultResourceFile;
            this.ParentFeatureFolder = ParentFeatureFolder;

            //this.Name = new ResourceStringContainer(string.Empty, DefaultResourceFile, ParentFeatureFolder);
            this.DisplayName = new ResourceStringContainer(string.Empty, DefaultResourceFile, ParentFeatureFolder);
            this.Group = new ResourceStringContainer(string.Empty, DefaultResourceFile, ParentFeatureFolder);

            if (ElementXml != null)
                this.PropXmlNode = ElementXml;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("ID"), ReadOnly(false), RefreshProperties(RefreshProperties.None)]
        public string ID
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Name"), RefreshProperties(RefreshProperties.None), ReadOnly(false)]
        public string Name
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Display name"), RefreshProperties(RefreshProperties.None), ReadOnly(false), TypeConverter(typeof(ResourceExpandableObjectConverter))]
        public ResourceStringContainer DisplayName
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Field type"), RefreshProperties(RefreshProperties.None), ReadOnly(false), TypeConverter(typeof(FieldTypeExpandableConverter))]
        public string Type
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Static name"), RefreshProperties(RefreshProperties.None), ReadOnly(false)]
        public string StaticName
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Group"), RefreshProperties(RefreshProperties.None), ReadOnly(false), TypeConverter(typeof(ResourceExpandableObjectConverter))]
        public ResourceStringContainer Group
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Read only"), RefreshProperties(RefreshProperties.None), ReadOnly(false)]
        public bool? ReadOnly
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Read only"), RefreshProperties(RefreshProperties.None), ReadOnly(false)]
        public bool? Hidden
        {
            get;
            set;
        }

        [CategoryAttribute("Properties"), DescriptionAttribute("Sealed"), RefreshProperties(RefreshProperties.None), ReadOnly(false)]
        public bool? Sealed
        {
            get;
            set;
        }

        public override string ToString()
        {
            return "<Field> " + this.Name;
        }

        public class FieldTypeExpandableConverter : ExpandableObjectConverter
        {
            public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
            {
                return false;
            }

            public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
            {
                return true;
            }

            public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
            {
                return true;
            }

            public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
            {
                return true;
            }

            public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
            {
                List<string> list = new List<string>();

                foreach (string s in Enum.GetNames(typeof(SPFieldType)))
                {
                    if (s != "Invalid")
                        list.Add(s);
                }

                return new StandardValuesCollection(list);
            }

            public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
            {
                return value;
            }

            public override bool GetPropertiesSupported(ITypeDescriptorContext context)
            {
                return false;
            }
        }

    }

    internal enum PredefinedListTypeEnum
    {
        GenericList,
        DocumentLibrary,
        Survey,
        Links,
        Announcements,
        Contacts,
        Events,
        Tasks,
        DiscussionBoard,
        PictureLibrary,
        WebPageLibrary,
        CustomGrid,
        GanttTasks,
        IssueTracking,
        XMLForm
    }

    internal struct BaseTypeInfoStruct
    {
        public string TemplateName;
        public int BaseType;
        public int Type;
        public string DisplayName;
        public string TemplateFilesPath;

        public BaseTypeInfoStruct(string TemplateName, int BaseType, int Type, string DisplayName, string TemplateFilesPath)
        {
            this.TemplateName = TemplateName;
            this.BaseType = BaseType;
            this.Type = Type;
            this.DisplayName = DisplayName;
            this.TemplateFilesPath = TemplateFilesPath;
        }
    }

}
